<?php
class Util {
	public $hostName = "localhost";
	public $userName = "root";
	public $passwrd = "";
	public $dbname = "pet_clinic";
	public $rootPath = "";
	public $perPage = 10;
	public $startLimit = 0;


	public function Util() {
		$this->connect();
		$this->selectDb();
		$this->rootPath = $_SERVER['DOCUMENT_ROOT'];
	}

	public function connect() {
		mysql_pconnect($this->hostName, $this->userName, $this->passwrd) or die("Incomplete connect");
		mysql_query("SET NAMES UTF8");
	}

	public function selectDb(){
		mysql_select_db($this->dbname);
	}

	public function fetchDataList($sqlQuery) {
		$arrData = array();
		$result = mysql_query($sqlQuery);
		if (!$result) {
			echo "Could not run query: " . mysql_error();
			exit;
		}
		if (mysql_num_rows($result) > 0) {
			while ($row = mysql_fetch_assoc($result)) {
				$arrData[] = $row;
			}
		}
		mysql_free_result($result);
		return $arrData;
	}

	public function fetchDataRow($sqlQuery) {
		$arrData = array();
		$result = mysql_query($sqlQuery);
		if (!$result) {
			echo "Could not run query: " . mysql_error();
			exit;
		}
		if (mysql_num_rows($result) > 0) {
			while ($row = mysql_fetch_assoc($result)) {
				$arrData = $row;
			}
		}
		mysql_free_result($result);
		return $arrData;
	}

	public function commitQuery($sqlQuery) {
		$result = mysql_query($sqlQuery);
		if (!$result) {
			echo "Could not run query: " . mysql_error();
			exit();
		} else {
			return "Y";
		}
	}

	public function queryLimit($page,$idTable) {
		if (isset($page) && trim($page) != "") {
			$this->startLimit = ($page - 1) * $this->perPage;
		}
		$queryLimit = " ORDER BY ".$idTable."  ASC LIMIT " . $this->startLimit . ", " . $this->perPage . " ";
		return $queryLimit;
	}

		public function getMaxPage($tableName) {
		$sqlQuery = "SELECT id FROM " .  $tableName;
		echo $sqlQuery;
		$result = mysql_query($sqlQuery);
		if (!$result) {
			echo "Could not run query: " . mysql_error();
			exit;
		}
		$numRow = mysql_num_rows($result);
		$maxPage = ceil($numRow / $this->perPage);
		return $maxPage;
	}
	public function getMaxPageByColumnName($tableName, $idColumn) {
		$sqlQuery = "SELECT ".$idColumn." FROM " .  $tableName;
		$result = mysql_query($sqlQuery);
		if (!$result) {
			echo "Could not run query: " . mysql_error();
			exit;
		}
		$numRow = mysql_num_rows($result);
		$maxPage = ceil($numRow / $this->perPage);
		return $maxPage;
	}
	
	public function getMaxPageByColumnNameForProduct($tableName, $idColumn) {
		$sqlQuery = "SELECT ".$idColumn." FROM " .  $tableName;
		$result = mysql_query($sqlQuery);
		if (!$result) {
			echo "Could not run query: " . mysql_error();
			exit;
		}
		$numRow = mysql_num_rows($result);
		$maxPage = ceil($numRow / 5);
		return $maxPage;
	}

	public function buildPageBtn($page, $maxPage) {
		$pageBtn = "<table width=\"100%\" >\n";
		$pageBtn .= "<tr class=\"ui-widget-content\" >\n";
		$pageBtn .= "<td width=\"10%\"><button onclick=\"location = '?page=1';\"><span class=\"ui-icon ui-icon-seek-prev\"></span></td>\n";
		if ($page > 1) {
			$pageBtn .= "<td width=\"40%\"><button onclick=\"location = '?page=" . ($page - 1) . "';\"><span class=\"ui-icon ui-icon-seek-first\"></span></td>\n";
		} else {
			$pageBtn .= "<td width=\"40%\"><button onclick=\"location = '?page=1';\"><span class=\"ui-icon ui-icon-seek-first\"></span></td>\n";
		}
		if ($page == $maxPage) {
			$pageBtn .= "<td width=\"40%\" align=\"right\"><button onclick=\"location = '?page=" . $maxPage . "';\"><span class=\"ui-icon ui-icon-seek-end\"></span></td>\n";
		} else {
			$pageBtn .= "<td width=\"40%\" align=\"right\"><button onclick=\"location = '?page=" . ($page + 1) . "';\"><span class=\"ui-icon ui-icon-seek-end\"></span></td>\n";
		}
		$pageBtn .= "<td width=\"10%\" align=\"right\"><button onclick=\"location = '?page=" . $maxPage . "';\"><span class=\"ui-icon ui-icon-seek-next\"></span></td>\n";
		$pageBtn .= "</tr>\n";
		$pageBtn .= "</table>\n";
		return $pageBtn;
	}
	
	public function array2json($arr) { 
    if(function_exists('json_encode')) return json_encode($arr); //Lastest versions of PHP already has this functionality. 
    $parts = array(); 
    $is_list = false; 

    //Find out if the given array is a numerical array 
    $keys = array_keys($arr); 
    $max_length = count($arr)-1; 
    if(($keys[0] == 0) and ($keys[$max_length] == $max_length)) {//See if the first key is 0 and last key is length - 1 
        $is_list = true; 
        for($i=0; $i<count($keys); $i++) { //See if each key correspondes to its position 
            if($i != $keys[$i]) { //A key fails at position check. 
                $is_list = false; //It is an associative array. 
                break; 
            } 
        } 
    } 

    foreach($arr as $key=>$value) { 
        if(is_array($value)) { //Custom handling for arrays 
            if($is_list) $parts[] = array2json($value); /* :RECURSION: */ 
            else $parts[] = '"' . $key . '":' . array2json($value); /* :RECURSION: */ 
        } else { 
            $str = ''; 
            if(!$is_list) $str = '"' . $key . '":'; 

            //Custom handling for multiple data types 
            if(is_numeric($value)) $str .= $value; //Numbers 
            elseif($value === false) $str .= 'false'; //The booleans 
            elseif($value === true) $str .= 'true'; 
            else $str .= '"' . addslashes($value) . '"'; //All other things 
            // :TODO: Is there any more datatype we should be in the lookout for? (Object?) 

            $parts[] = $str; 
        } 
    } 
    $json = implode(',',$parts); 
     
    if($is_list) return '[' . $json . ']';//Return numerical JSON 
    return '{' . $json . '}';//Return associative JSON 
} 
}
?>